package com.ytjj.quartz.service;

import com.ytjj.qmyx.mall.mapper.ProductCategoryMapper;
import com.ytjj.qmyx.mall.mapper.ProductMapper;
import com.ytjj.qmyx.mall.mapper.ProductStatisticsMapper;
import com.ytjj.qmyx.mall.model.Product;
import com.ytjj.qmyx.mall.model.ProductCategory;
import com.ytjj.qmyx.mall.model.ProductExample;
import com.ytjj.qmyx.mall.model.ProductStatistics;
import com.ytjj.quartz.dao.OrdersInfoDao;
import com.ytjj.quartz.dao.ProductBrowsingRecordDao;
import com.ytjj.quartz.dao.ProductStatisticsDao;
import com.ytjj.quartz.model.OrdersInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class ProductStatisticsService {

    @Resource
    private OrdersInfoDao ordersInfoDao;

    @Resource
    private ProductMapper productMapper;

    @Resource
    private ProductCategoryMapper productCategoryMapper;

    @Resource
    private ProductBrowsingRecordDao productBrowsingRecordDao;

    @Resource
    private ProductStatisticsMapper productStatisticsMapper;

    @Resource
    private ProductStatisticsDao productStatisticsDao;

    @Transactional(rollbackFor = Exception.class)
    public void productStatistics() {
        // 昨天的时间
        Calendar cal=Calendar.getInstance();
        cal.add(Calendar.DATE,-1);
        Date date = cal.getTime();
        // 上架商品
        List<Product> productList = productMapper.selectByExample(new ProductExample());
        productList.stream().forEach(item ->{

            // 查询该商品统计是否统计过该商品
            ProductStatistics productStatistics = productStatisticsDao.selectIsStatistic(item.getId(),date);
            if(productStatistics == null){
                productStatistics = new ProductStatistics();
                // 商品id
                productStatistics.setProductId(item.getId());
                // 商品名称
                productStatistics.setProductName(item.getProductName());
                // 商品的1级分类
                ProductCategory productCategory = productCategoryMapper.selectByPrimaryKey(item.getCategoryId());
                productStatistics.setTopCategory(productCategory.getPid());
                // 商品日pv
                Integer pv = productBrowsingRecordDao.selectProductBrowsingRecordList(item.getId());
                productStatistics.setProductPv(pv);
                // 商品日uv
                Integer uv = productBrowsingRecordDao.selectProductBrowsingRecordUv(item.getId());
                productStatistics.setProductUv(uv);

                // 订单数
                List<OrdersInfo> ordersInfos = ordersInfoDao.selectNumYesterDay(item.getId());
                productStatistics.setOrderNum(ordersInfos.size());

                // 实际订单数
                List<OrdersInfo> realOrdersInfos = ordersInfoDao.selectRealNumYesterDay(item.getId());
                productStatistics.setRealOrderNum(realOrdersInfos.size());

                // 支付订单数
                List<OrdersInfo> payOrdersInfos = ordersInfos.stream().filter(oi -> oi.getIsPay() == 1).collect(Collectors.toList());
                productStatistics.setPayOrderNum(payOrdersInfos.size());

                if(pv == 0){
                    productStatistics.setOrderRate(BigDecimal.ZERO);
                    productStatistics.setPayOrderRatio(BigDecimal.ZERO);
                }else{
                    BigDecimal pbrpv = new BigDecimal(pv);
                    // 订单率 (订单数/pv）
                    BigDecimal realOrderNum = new BigDecimal(realOrdersInfos.size());
                    BigDecimal orderRate = realOrderNum.divide(pbrpv,2,BigDecimal.ROUND_HALF_UP);
                    productStatistics.setOrderRate(orderRate);

                    // 支付率 (支付订单/总订单)
                    BigDecimal payOrderNum = new BigDecimal(payOrdersInfos.size());
                    if(ordersInfos.size() == 0){
                        productStatistics.setPayOrderRatio(BigDecimal.ZERO);
                    }else{
                        BigDecimal totalOrderNum = new BigDecimal(ordersInfos.size());
                        BigDecimal payOrderRatio = payOrderNum.divide(totalOrderNum,2,BigDecimal.ROUND_HALF_UP);
                        productStatistics.setPayOrderRatio(payOrderRatio);
                    }
                }

                // 统计的天数
                productStatistics.setStatisticsTime(date);

                productStatisticsMapper.insert(productStatistics);
            }else{
                // 商品日pv
                Integer pv = productBrowsingRecordDao.selectProductBrowsingRecordList(item.getId());
                productStatistics.setProductPv(pv);

                // 商品日uv
                Integer uv = productBrowsingRecordDao.selectProductBrowsingRecordUv(item.getId());
                productStatistics.setProductUv(uv);

                // 订单数
                List<OrdersInfo> ordersInfos = ordersInfoDao.selectNumYesterDay(item.getId());
                productStatistics.setOrderNum(ordersInfos.size());

                // 实际订单数
                List<OrdersInfo> realOrdersInfos = ordersInfoDao.selectRealNumYesterDay(item.getId());
                productStatistics.setRealOrderNum(realOrdersInfos.size());

                // 支付订单数
                List<OrdersInfo> payOrdersInfos = ordersInfos.stream().filter(oi -> oi.getIsPay() == 1).collect(Collectors.toList());
                productStatistics.setPayOrderNum(payOrdersInfos.size());

                if(pv == 0){
                    productStatistics.setOrderRate(BigDecimal.ZERO);
                    productStatistics.setPayOrderRatio(BigDecimal.ZERO);
                }else{
                    BigDecimal pbrpv = new BigDecimal(pv);
                    // 订单率 (订单数/pv）
                    BigDecimal realOrderNum = new BigDecimal(realOrdersInfos.size());
                    BigDecimal orderRate = realOrderNum.divide(pbrpv,2,BigDecimal.ROUND_HALF_UP);
                    productStatistics.setOrderRate(orderRate);

                    // 支付率 (支付订单/总订单)
                    BigDecimal payOrderNum = new BigDecimal(payOrdersInfos.size());
                    BigDecimal totalOrderNum = new BigDecimal(ordersInfos.size());
                    BigDecimal payOrderRatio = payOrderNum.divide(totalOrderNum,2,BigDecimal.ROUND_HALF_UP);
                    productStatistics.setPayOrderRatio(payOrderRatio);
                }

                // 统计的天数
                productStatistics.setStatisticsTime(date);

                productStatisticsMapper.updateByPrimaryKeySelective(productStatistics);
            }
        });

    }

    @Transactional(rollbackFor = Exception.class)
    public void productStatisticsNow() {
        // 今天的时间
        Date date = new Date();
        // 上架商品
        List<Product> productList = productMapper.selectByExample(new ProductExample());
        productList.stream().forEach(item ->{

            // 查询该商品统计是否统计过该商品
            ProductStatistics productStatistics = productStatisticsDao.selectIsStatistic(item.getId(),date);
            if(productStatistics == null){
                productStatistics = new ProductStatistics();
                // 商品id
                productStatistics.setProductId(item.getId());
                // 商品名称
                productStatistics.setProductName(item.getProductName());
                // 商品的1级分类
                ProductCategory productCategory = productCategoryMapper.selectByPrimaryKey(item.getCategoryId());
                productStatistics.setTopCategory(productCategory.getPid());
                // 商品日pv
                Integer pv = productBrowsingRecordDao.selectProductBrowsingRecordPvNow(item.getId());
                productStatistics.setProductPv(pv);
                // 商品日uv
                Integer uv = productBrowsingRecordDao.selectProductBrowsingRecordUvNow(item.getId());
                productStatistics.setProductUv(uv);

                // 订单数
                List<OrdersInfo> ordersInfos = ordersInfoDao.selectNumNowDay(item.getId());
                productStatistics.setOrderNum(ordersInfos.size());

                // 实际订单数
                List<OrdersInfo> realOrdersInfos = ordersInfoDao.selectRealNumNowDay(item.getId());
                productStatistics.setRealOrderNum(realOrdersInfos.size());

                // 支付订单数
                List<OrdersInfo> payOrdersInfos = ordersInfos.stream().filter(oi -> oi.getIsPay() == 1).collect(Collectors.toList());
                productStatistics.setPayOrderNum(payOrdersInfos.size());

                if(pv == 0){
                    productStatistics.setOrderRate(BigDecimal.ZERO);
                    productStatistics.setPayOrderRatio(BigDecimal.ZERO);
                }else{
                    BigDecimal pbrpv = new BigDecimal(pv);
                    // 订单率 (订单数/pv）
                    BigDecimal realOrderNum = new BigDecimal(realOrdersInfos.size());
                    BigDecimal orderRate = realOrderNum.divide(pbrpv,2,BigDecimal.ROUND_HALF_UP);
                    productStatistics.setOrderRate(orderRate);

                    // 支付率 (支付订单/总订单)
                    BigDecimal payOrderNum = new BigDecimal(payOrdersInfos.size());
                    BigDecimal totalOrderNum = new BigDecimal(ordersInfos.size());
                    BigDecimal payOrderRatio = new BigDecimal(0);
                    if(totalOrderNum.compareTo(BigDecimal.ZERO) > 0){
                        payOrderRatio = payOrderNum.divide(totalOrderNum,2,BigDecimal.ROUND_HALF_UP);
                    }
                    productStatistics.setPayOrderRatio(payOrderRatio);
                }

                // 统计的天数
                productStatistics.setStatisticsTime(date);

                productStatisticsMapper.insert(productStatistics);
            }else{
                // 商品日pv
                Integer pv = productBrowsingRecordDao.selectProductBrowsingRecordPvNow(item.getId());
                productStatistics.setProductPv(pv);
                // 商品日uv
                Integer uv = productBrowsingRecordDao.selectProductBrowsingRecordUvNow(item.getId());
                productStatistics.setProductUv(uv);

                // 订单数
                List<OrdersInfo> ordersInfos = ordersInfoDao.selectNumNowDay(item.getId());
                productStatistics.setOrderNum(ordersInfos.size());

                // 实际订单数
                List<OrdersInfo> realOrdersInfos = ordersInfoDao.selectRealNumNowDay(item.getId());
                productStatistics.setRealOrderNum(realOrdersInfos.size());

                // 支付订单数
                List<OrdersInfo> payOrdersInfos = ordersInfos.stream().filter(oi -> oi.getIsPay() == 1).collect(Collectors.toList());
                productStatistics.setPayOrderNum(payOrdersInfos.size());

                if(pv == 0){
                    productStatistics.setOrderRate(BigDecimal.ZERO);
                    productStatistics.setPayOrderRatio(BigDecimal.ZERO);
                }else{
                    BigDecimal pbrpv = new BigDecimal(pv);
                    // 订单率 (订单数/pv）
                    BigDecimal realOrderNum = new BigDecimal(realOrdersInfos.size());
                    BigDecimal orderRate = realOrderNum.divide(pbrpv,2,BigDecimal.ROUND_HALF_UP);
                    productStatistics.setOrderRate(orderRate);

                    // 支付率 (支付订单/总订单)
                    BigDecimal payOrderNum = new BigDecimal(payOrdersInfos.size());
                    if(ordersInfos.size() == 0){
                        productStatistics.setPayOrderRatio(BigDecimal.ZERO);
                    }else{
                        BigDecimal totalOrderNum = new BigDecimal(ordersInfos.size());
                        BigDecimal payOrderRatio = payOrderNum.divide(totalOrderNum,2,BigDecimal.ROUND_HALF_UP);
                        productStatistics.setPayOrderRatio(payOrderRatio);
                    }
                }

                // 统计的天数
                productStatistics.setStatisticsTime(date);

                productStatisticsMapper.updateByPrimaryKeySelective(productStatistics);
            }
        });
    }

}
